package leetcode.秋招.阿里;

import java.util.Scanner;

/**
 * @author ：zsy
 * @date ：Created 2021/8/2 18:57
 * @description：
 */
public class Main {
    static int[][] map;
    final static int[][] dirs = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
    static int ans;
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int T = in.nextInt();
        for (int i = 0; i < T; i++) {
            ans = Integer.MAX_VALUE;
            int n = in.nextInt();
            int m = in.nextInt();
            map = new int[n][m];
            for (int j = 0; j < n; j++) {
                for (int k = 0; k < m; k++) {
                    map[j][k] = in.nextInt();
                }
            }
            dfs(0, 0, 0);
            if (ans <= map[n - 1][m - 1]) {
                System.out.println("YES");
            } else
                System.out.println("NO");
        }
    }

    private static void dfs(int row, int col, int time) {
        if (time > map[row][col]) return;
        if (row == map.length - 1 && col == map[0].length - 1) {
            ans = Math.min(ans, time);
            return;
        }
        if (ans != Integer.MAX_VALUE) return;
        for (int[] dir : dirs) {
            int curRow = row + dir[0];
            int curCol = col + dir[1];
            if (curRow < map.length && curCol < map[0].length) {
                dfs(curRow, curCol, time + 1);
            }
        }
    }
}
